<?php

namespace app\admin\server\music;

use app\admin\model\music\MusicSkewerModel;
use app\admin\model\music\MusicSkewerTypeModel;
use app\admin\model\music\MusicWelfareModel;
use app\admin\ServerBase;
use app\admin\validate\music\MusicSkewerValidate;
use app\admin\validate\music\MusicWelfareVal;
use app\logic\single\GoodsSingle;
use think\db\exception\DbException;
use think\facade\Db;
use think\Request;

class MusicWelfareServer extends ServerBase
{
    protected $goodsType=[40,50];
    function __construct( Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new MusicWelfareModel();
        $this->validate = new MusicWelfareVal();
    }


    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        [$list,$count] = $this->model->getList($this->request,[],'*',$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }

    function getDetail(){
        $id =  $this->request->param('id');
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        $info['goods'] = GoodsSingle::getInstance()->getGoodsByType($info['goods_id'],$info['type']);
        $this->getMessageCode('adminMessage.success.select',2);
        return ['info'=>$info];
    }
    function add(){
        $post = $this->request->post();
        if(!$this->validate->scene('create')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        Db::startTrans();
        try {
            //验证商品是否存在和type是否合法
            $res = $this->checkGoods($post);
            if(is_string($res)){
                $this->ERROR = $res;
                return false;
            }
            $this->model->save($post);
            Db::commit();
            $this->getMessageCode('adminMessage.success.insert',2);
            return true;
        }catch (\Error $error){
            Db::rollback();
            $this->ERROR = $error->getMessage();
            return false;
        }catch (DbException $dbException){
            Db::rollback();
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }
    function edit(){
        $id = $this->request->param('id',0);
        $post = $this->request->post();
        $post['id'] = $id;
        if(!$this->validate->scene('update')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        $info  = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
        Db::startTrans();
        try {
            //验证商品是否存在和type是否合法
            $res = $this->checkGoods($post);
            if(is_string($res)){
                $this->ERROR = $res;
                return false;
            }
            $info->save($post);
            Db::commit();
            $this->getMessageCode('adminMessage.success.update',2);
            return true;
        }catch (\Error $error){
            Db::rollback();
            $this->ERROR = $error->getMessage();
            return false;
        }catch (DbException $dbException){
            Db::rollback();
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }

    private function checkGoods($post)
    {
        if(!in_array($post['type'],$this->goodsType)){
            return '商品类型不合法';
        }
        $goods = GoodsSingle::getInstance()->getGoodsByType($post['goods_id'],$post['type']);
        if(empty($goods)){
            return '音乐不存在';
        }
        return true;
    }
}